/*
 * Copyright (C) 2011 Fhpt All Rights Reserved.
 * 
 * UserLoginCtrl.java
 */
package com.pay.sms.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pay.sms.MoCtrl;
import com.pay.sms.model.TUser;
import com.pay.sms.model.TUserAccount;
import com.pay.sms.model.TUserBill;
import com.pay.sms.model.TUserBlackWhite;
import com.pay.sms.model.TUserKou;
import com.pay.sms.model.UserVO;
import com.pay.sms.service.AccountService;
import com.pay.sms.service.BillService;
import com.pay.sms.service.BlackWhiteService;
import com.pay.sms.service.KouService;
import com.pay.sms.service.UserService;

@Controller
@RequestMapping("/user")
public class UserCtrl extends MoCtrl{
	@Autowired
	private UserService userService;
//	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private KouService kouService;
	
	@Autowired
	private BillService billService;
	
	@Autowired
	private BlackWhiteService blackWhiteService;

	@RequestMapping("/index.htm")
	public String page(TUser t,Integer page,HttpServletRequest request) {
//		try {
//			t.setProxyUserId(WebUtil.useId(request)!=null?WebUtil.useId(request).toString():null);
//			t.setType(UserType.common.getCode());
//			Page<User> pageList =  userService.pageList(t, PageUtil.get(page));
//			pageModel(model, page, pageList,title);
//		} catch (Exception e) {
//			logger.error(e);
//		}
		return "views/sms/user/index";
	}
	
	@RequestMapping("/enter.htm")
	public String enter(String id,HttpServletRequest request)
	{
		String act = request.getParameter("act");
		if(act.equals("add")){
			request.setAttribute("user", new TUser());
			return "views/sms/user/edit";
		}else if(act.equals("edit")){
			TUser user = userService.get(Long.valueOf(id));
			request.setAttribute("user", user);
			return "views/sms/user/index";
		}else if(act.equals("remove")){
			return "redirect:/user/userlist";
		}else{
			return null;
		}
		
	}
	
//	@RequestMapping("/del")
//	public String del(Model model,Long id,HttpServletRequest request)
//	{
//		User t = userService.get(id);
//		userService.delete(id);
//		//暂时不删除起账户，账户有余额
////		Account  acct  = accountService.getUserAccount(id);
////		accountService.delete(acct.getId());
//		//删除费率
//		//chanel_fee
//		
//		operLogService.add(new OperLog(WebUtil.useId(request), WebUtil.getLogStatus(request), "删除用户！用户名："+(t!=null?t.getUserName():"-")));
//		return "redirect:/user/userlist";
//	}
//	
//	@RequestMapping("/edit")
//	public String view(Model model,Long id,HttpServletRequest request)
//	{
//		if (id != null) {
//			model.addAttribute("entity", userService.get(id));
//		}
//		model.addAttribute("show", request.getParameter("show"));
//		request.setAttribute("chanelList",messageChannelService.query(new MessageChannel()) );
//		model.addAttribute("title", title);
//		return "sms/user/edit";
//	}
//	
	@RequestMapping("/save")
	public String save(UserVO t,HttpServletRequest request)
	{
		TUser user = new TUser();
		TUserAccount acct = new TUserAccount();
		TUserBill bill = new TUserBill();
		TUserKou kou = new TUserKou();
		TUserBlackWhite blackwhite = new TUserBlackWhite();
		if(t.getUid()==null) {
			//执行插入
			try { 
				BeanUtils.copyProperties(user, t);
				BeanUtils.copyProperties(acct, t);
				BeanUtils.copyProperties(bill, t);
				BeanUtils.copyProperties(kou, t);
				BeanUtils.copyProperties(blackwhite, t);
				userService.add(user);
				kou.setUid(user.getUid());
				kouService.add(kou);
				blackwhite.setUid(user.getUid());
				blackWhiteService.add(blackwhite);
				acct.setUid(user.getUid());
				accountService.add(acct);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else {
			//执行修改
		}
		return "redirect:/user/userlist";
	}
//	
//	@RequestMapping("/zore")
//	public String zoreUser(Model model,Long id,HttpServletRequest request)
//	{
//		User t = userService.get(id);
//		t.setStatus(UserStatus.zore.getCode());
//		userService.update(t);
//		operLogService.add(new OperLog(WebUtil.useId(request), WebUtil.getLogStatus(request), "冻结用户！用户名："+(t!=null?t.getUserName():"-")));
//		return "redirect:/user/page";
//	}
//	
//	@RequestMapping("/pwd")
//	public String pwd(Model model,Long id,HttpServletRequest request)
//	{
//		return "sms/config/password";
//	}
//	
//	@RequestMapping("/savepwd")
//	public String savepwd(Model model,User t,String newpassword,HttpServletRequest request)
//	{
//		User user =(User)request.getSession().getAttribute("userInfo");
//		if(user.getPasswords().equals(request.getParameter("password"))){
//			//修改密码
//			user.setPasswords(newpassword);
//			userService.update(user);
//			operLogService.add(new OperLog(WebUtil.useId(request), WebUtil.getLogStatus(request), "密码修改！用户名："+user.getUserName()));
//		} 
//		return "redirect:/user/pwd";
//	}
//	
//	
//	@RequestMapping("/login")
//	public String login(Model model,User t,HttpServletRequest request)
//	{
//		
//		User user = userService.getUserName(new User(t.getUserName()));
//		
//		if (user != null) {
//
//			if(user.getPasswords().equals(t.getPasswords()) && user.getStatus() == 1)
//			{
//				operLogService.add(new OperLog(user.getId(), WebUtil.getLogStatus(request), "登录成功！用户名："+user.getUserName(), request.getLocalAddr()));
//				request.getSession().setAttribute("userInfo", user);//保存实体信息
//				request.getSession().setAttribute("user.id", user.getId());
//				request.getSession().setAttribute("loginname", user.getLoginName());
//				request.getSession().setAttribute("username", user.getUserName());
//				request.getSession().setAttribute("type", user.getType());
//				
//				return "main";
//			}
//		} 
//		
//		model.addAttribute("message", "登陆失败,用户名和密码输入有误");
//		request.setAttribute("backURL", "/user/logout");
//		
//		return "tip";
//	}
//
//	@RequestMapping("/logout")
//	public String logout(Model model,User t,HttpServletRequest request)
//	{
//		
//		User user = (User)request.getSession().getAttribute("userInfo");
//		if(user != null){
//			operLogService.add(new OperLog(user.getId(), WebUtil.getLogStatus(request), "退出系统！用户名："+user.getUserName(), request.getLocalAddr()));
//			request.getSession().setAttribute("userInfo", null);//保存实体信息
//			request.getSession().setAttribute("user.id", null);
//			request.getSession().setAttribute("username", null);
//			request.getSession().setAttribute("loginname", null);
//			request.getSession().setAttribute("type",null);
//		}
//		return "/login";
//	}
//	
//	@RequestMapping("/welcome")
//	public String welcome(HttpServletRequest request)
//	{
//		WelcomeContent wc = userService.getWC();
//		request.setAttribute("wc", wc);
//		return "sms/user/welcome";
//	}
//	
//	@RequestMapping("/updatewelcome")
//	public String saveContent(HttpServletRequest request)
//	{
//		String content =  request.getParameter("content");
//		Long id = Long.valueOf(request.getParameter("id"));
//		WelcomeContent wc = new WelcomeContent();
//		wc.setId(id);
//		wc.setContent(content);
//		userService.saveContent(wc);
//		return "redirect:/user/welcome";
//	}
//	
//	@RequestMapping("/addUser.htm")
//	public String addUser(Long id,HttpServletRequest request)
//	{
////		IF (ID != NULL) {
////			REQUEST.SETATTRIBUTE("ENTITY", USERSERVICE.GET(ID));
////		} 
////		request.setAttribute("chanelList",messageChannelService.query(new MessageChannel()) );
////		request.setAttribute("title", title);
//		return "views/sms/user/addUser";
//	}
//	
//	@RequestMapping("/saveAll")
//	public String saveAll(User t,HttpServletRequest request)
//	{
////		String[] chnalId = request.getParameterValues("chn");
////		String[] fees = request.getParameterValues("feelv");
//		Long fee = Long.valueOf(request.getParameter("feelv"));
//		Long currentUserId = WebUtil.useId(request);
//		t.setProxyUserId(currentUserId.toString());
//		t.setShortName(t.getUserName());
//		t.setPrice(fee);
//		userService.add(t);
//		
//		/////////////////////
////		for(int i = 0;i<chnalId.length;i++){
////			ChannelRate rate = new ChannelRate();
////			rate.setChannelId(Long.valueOf(chnalId[i]));
////			rate.setPrice(Long.valueOf(fees[i]==null?"0":fees[i]));
////			rate.setUserId(t.getId());
////			rate.setProxyUserId(currentUserId);
////			channelRateService.saveChanel(rate);
////		}
//		
//		Account my = new Account();
//		my.setUserId(t.getId());
//		my.setProxyUserId(currentUserId);
//		my.setAccount(0L);
//		my.setPrice(0L);
//		accountService.add(my);
//		
//		return "redirect:/user/addAllUser";
//	}
//	
//	@RequestMapping("/userlist")
//	public String userlist(Model model,User t,Integer page,HttpServletRequest request)
//	{
//		try {
////			Integer type = (Integer)request.getSession().getAttribute("type");
////			t.setType(type);
//			Long currentUserId = WebUtil.useId(request);
//			t.setId(currentUserId);
//			Page<User> pageList = userService.pageList(t, PageUtil.get(page));
//			pageModel(model, page, pageList,title);
//		} catch (Exception e) {
//			logger.error(e);
//		} 
//		return "sms/user/userlist";
//	}
//	
//	@RequestMapping("/feilv")
//	public String feilv(HttpServletRequest request)
//	{
//		try {
//			Long currentUserId = Long.valueOf(request.getParameter("userId"));
//			List<ChannelRate> crList = channelRateService.queryfee(new ChannelRate(currentUserId));
//			for(ChannelRate cr : crList){
//				cr.setChannelIdName(messageChannelService.get(cr.getChannelId()).getName()); 
//			}
//			request.setAttribute("userId", currentUserId);
//			request.setAttribute("crList", crList);
//		} catch (Exception e) {
//			logger.error(e);
//		} 
//		return "sms/user/koulianglist";
//	} 
//	
//	
//	@RequestMapping("/saverate")
//	public String saverate(HttpServletRequest request)
//	{
//		try {
//			Long currentUserId = Long.valueOf(request.getParameter("userId"));
//			String[] id = request.getParameterValues("pid");
//			String[] chnalId = request.getParameterValues("channelId"); 
//			String[] price  = request.getParameterValues("price");
//			String[] proxyUserId  = request.getParameterValues("proxyUserId");
//			for(int i = 0;i<id.length;i++){
//				ChannelRate rate = new ChannelRate();
//				rate.setId(Long.valueOf(id[i]));
//				rate.setChannelId(Long.valueOf(chnalId[i]));
//				rate.setPrice(Long.valueOf(price[i]==null?"0":price[i]));
//				rate.setUserId(currentUserId);
//				rate.setProxyUserId(Long.valueOf(proxyUserId[i]));
//				channelRateService.updateChanel(rate);
//			}
//			 
//			
//		} catch (Exception e) {
//			logger.error(e);
//		} 
//		return "redirect:/user/userlist";
//	} 
}
